package com.example.composableexample

import android.util.Log
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.combinedClickable
import androidx.compose.foundation.gestures.Orientation
import androidx.compose.foundation.gestures.detectTapGestures
import androidx.compose.foundation.gestures.draggable
import androidx.compose.foundation.gestures.rememberDraggableState
import androidx.compose.foundation.gestures.rememberScrollableState
import androidx.compose.foundation.gestures.scrollable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.offset
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.material.ExperimentalMaterialApi
import androidx.compose.material.FractionalThreshold
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Add
import androidx.compose.material.rememberSwipeableState
import androidx.compose.material.swipeable
import androidx.compose.material3.Card
import androidx.compose.material3.DropdownMenu
import androidx.compose.material3.DropdownMenuItem
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.IntOffset
import androidx.compose.ui.unit.dp
import kotlin.math.roundToInt

/**
 * author: LiLiang
 * createTime: 2022/9/21-23:16
 * remark:
 **/

@Preview
@Composable
fun CardWithMenu() {

    var expanded by remember {
        mutableStateOf(false)
    }

    Surface(modifier = Modifier.fillMaxSize(), color = Color.Gray) {
        Row {
            Card(
                modifier = Modifier
                    .fillMaxWidth()
                    .height(80.dp)
                    .pointerInput(Unit) {
                        detectTapGestures(
                            onDoubleTap = { println("onDoubleTap") },
                            onLongPress = { println("onLongPress") },
                            onPress = { println("onPress") },
                            onTap = { println("onTap") }
                        )
                    }
            ) {
                Row(
                    verticalAlignment = Alignment.CenterVertically,
                    modifier = Modifier.fillMaxSize(),
                    horizontalArrangement = Arrangement.SpaceBetween
                ) {
                    Text(text = "TopAppBar", modifier = Modifier.weight(1F))
                    Box {
                        Image(
                            imageVector = Icons.Default.Add,
                            contentDescription = "",
                            modifier = Modifier
                                .clickable { expanded = !expanded })
                        DropdownMenu(expanded = expanded, onDismissRequest = { expanded = false }) {
                            DropdownMenuItem(text = { Text(text = "创建群聊") }, onClick = { /*TODO*/ })
                            DropdownMenuItem(text = { Text(text = "扫一扫") }, onClick = { /*TODO*/ })
                        }
                    }
                }
            }
        }
    }
}

/**
 * 滚动手势
 */
@Preview
@Composable
fun ScrollableSample() {
    var offset by remember { mutableStateOf(0f) }
    Box(
        Modifier
            .size(150.dp)
            .scrollable(
                orientation = Orientation.Vertical,
                // Scrollable state: describes how to consume
                // scrolling delta and update offset
                state = rememberScrollableState { delta ->
                    offset += delta
                    delta
                }
            )
            .background(Color.LightGray),
        contentAlignment = Alignment.Center
    ) {
        Text(offset.toString())
    }
}

/**
 * 点击
 */
@OptIn(ExperimentalFoundationApi::class)
@Preview
@Composable
fun CombinedClickDemo() {
    var enableState by remember {
        mutableStateOf(true)
    }
    Box(modifier = Modifier
        .size(200.dp)
        .background(Color.Green)
        .combinedClickable(
            enabled = enableState,
            onLongClick = {
                Log.d("TAG", "发生长按点击操作了～")
            },
            onDoubleClick = {
                Log.d("TAG", "发生双击操作了～")
            },
            onClick = {
                Log.d("TAG", "发生单击操作了～")
            }
        )
    )
}

/**
 * 拖动手势
 */
@Preview
@Composable
fun DraggableSimple() {
    var offsetX by remember {
        mutableStateOf(0f)
    }
    val boxSideLengthDp = 50.dp
    val boxSildeLengthPx = with(LocalDensity.current) {
        boxSideLengthDp.toPx()
    }
    val draggableState = rememberDraggableState {
        offsetX = (offsetX + it).coerceIn(0f, 3 * boxSildeLengthPx)
    }

    Column {
        Box(
            Modifier
                .width(boxSideLengthDp * 4)
                .height(boxSideLengthDp)
                .background(Color.LightGray)
        ) {
            Box(
                Modifier
                    .size(boxSideLengthDp)
                    .offset {
                        IntOffset(offsetX.roundToInt(), 0)
                    }
                    .draggable(
                        orientation = Orientation.Horizontal,
                        state = draggableState
                    )
                    .background(Color.DarkGray)

            )
        }
        if(offsetX == 3*boxSildeLengthPx) {
            Text(text = "到头了")
        }
    }

}

enum class Status{
    CLOSE, OPEN
}

@ExperimentalMaterialApi
@Preview
@Composable
fun SwipeableDemo() {
    var blockSize = 48.dp
    var blockSizePx = with(LocalDensity.current) { blockSize.toPx() }
    var swipeableState = rememberSwipeableState(initialValue = Status.CLOSE)
    var anchors = mapOf(
        0f to Status.CLOSE,
        blockSizePx to Status.OPEN
    )
    Box(
        modifier = Modifier
            .size(height = blockSize, width = blockSize * 5)
            .background(Color.LightGray)
    ) {
        Box(
            modifier = Modifier
                .offset {
                    IntOffset(swipeableState.offset.value.toInt(), 0)
                }
                .swipeable(
                    state = swipeableState,
                    anchors = mapOf(
                        0f to Status.CLOSE,
                        blockSizePx * 4 to Status.OPEN
                    ),
                    thresholds = { from, to ->
                        if (from == Status.CLOSE) {
                            FractionalThreshold(0.7f)
                        } else {
                            FractionalThreshold(0.7f)
                        }
                    },
                    orientation = Orientation.Horizontal
                )
                .size(blockSize)
                .background(Color.DarkGray)
        )
    }
}